package com.util;


import hk.MyUser;

import java.sql.*;

public class JdbcUtil {
    public MyUser getUserByNameAndPsw(String username,String passward) throws ClassNotFoundException,SQLException{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/hk?characterEncoding=utf8&useSSL=false","root","000000");

        String sql=" select id,username,passward from user where username=? and passward=?";
        ps=conn.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2,passward);

        rs=ps.executeQuery();

        while (rs.next()){
           MyUser user=new MyUser();
            user.setId(rs.getInt(1));
            user.setName(rs.getString(2));
            user.setPassward(rs.getString(3));



            return user;

        }


                rs.close();
                conn.close();
                ps.close();
                return null;
    }

    public static void main(String[] args) throws Exception{
        JdbcUtil jdbcUtil=new JdbcUtil();
        MyUser user=jdbcUtil.getUserByNameAndPsw("a1","123");
        System.out.println(user);
    }
}
